//
//  YZMHomeUsersModel.m
//  YZMicroStock
//
//  Created by apple on 16/11/8.
//  Copyright © 2016年 cqjr. All rights reserved.
//

#import "YZMHomeUsersModel.h"

#define sYZMHomeUsersModelDefault @"YZMHomeUsersModelDefault"

@implementation YZMHomeUsersModel

+(NSDictionary *)JSONKeyPathsByPropertyKey
{
    return @{
             @"userName":@"userName",
             @"stockName":@"stockName",
             @"volumeOrder":@"volumeOrder",
             @"profitTotal":@"profitTotal",
             };
}

- (void)setNilValueForKey:(NSString *)key {
    
}

+ (void)createEncoder:(NSArray *)array {
    
    if (!array || array.count == 0) {
        
        return;
    }
    
    NSMutableArray *archiverArray = [NSMutableArray array];
    
    for (YZMHomeUsersModel *instance in array) {
        
        NSMutableData *data = [NSMutableData data];
        //创建一个归档类
        NSKeyedArchiver *archiver = [[NSKeyedArchiver alloc] initForWritingWithMutableData:data];
        [archiver encodeObject:instance forKey:sYZMHomeUsersModelDefault];
        [archiver finishEncoding];
        
        [archiverArray addObject:data];
    }
    
    //将数据写入文件里
    [[NSUserDefaults standardUserDefaults] setObject:archiverArray forKey:sYZMHomeUsersModelDefault];
    [[NSUserDefaults standardUserDefaults] synchronize];
}

+ (NSMutableArray *)createDecoder {
    
    NSArray *array = [[NSUserDefaults standardUserDefaults] objectForKey:sYZMHomeUsersModelDefault];
    
    if (!array ||array.count == 0) {
        
        return nil;
    }
    
    NSMutableArray *tempArray = [NSMutableArray array];
    
    for (NSData *data in array) {
        
        if (data && data.length != 0) {
            
            NSKeyedUnarchiver *unarchiver = [[NSKeyedUnarchiver alloc] initForReadingWithData:data];
            
            YZMHomeUsersModel *instance = [unarchiver decodeObjectForKey:sYZMHomeUsersModelDefault];
            [unarchiver finishDecoding];
            
            [tempArray addObject:instance];
        }
    }
    
    if (tempArray.count == 0) {
        
        return nil;
    }
    
    return tempArray;
}

@end
